package com.justech.eat.service.event.listener;

import com.justech.eat.entity.Order;
import com.justech.eat.entity.OrderDetail;
import com.justech.eat.entity.OrderLog;
import com.justech.eat.service.OrderLogService;
import com.justech.eat.service.event.OrderBookSubmitEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: eat-service
 * @description: 报餐提交事件处理器
 * @author: feifei.li
 * @create: 2020-06-04 18:21
 **/
@Component
public class OrderBookSubmitListener implements ApplicationListener<OrderBookSubmitEvent> {
    @Autowired
    OrderLogService orderLogService;

    @Override
    public void onApplicationEvent(OrderBookSubmitEvent orderBookSubmitEvent) {
        Order order = orderBookSubmitEvent.getOrder();
        ArrayList<OrderLog> orderLogs = new ArrayList<>();
        List<OrderDetail> orderDetails = order.getOrderDetails();
        for (OrderDetail orderDetail : orderDetails) {
            OrderLog orderLog = new OrderLog();
            orderLog.setDoType("save");
            orderLog.setOrderDetailId(orderDetail.getId());
            orderLog.setOrderId(order.getId());
            orderLog.setType(orderDetail.getType());
            orderLogs.add(orderLog);
        }
        orderLogService.saveAll(orderLogs);
    }
}
